<template>
  <div>
    <h1>修改密码</h1>
    <div style="margin: 20px" />
    <el-form
      ref="form_password"
      :label-position="labelPosition"
      label-width="100px"
      :model="form_password"
      style="max-width: 460px"
    >
      <el-form-item prop="ypassword" label="原密码">
        <el-input
          v-model="form_password.ypassword"
          prefix-icon="el-icon-lock"
          show-password
        />
      </el-form-item>
      <el-form-item prop="xpassword" label="新密码">
        <el-input
          v-model="form_password.xpassword"
          prefix-icon="el-icon-lock"
          show-password
        />
      </el-form-item>
      <el-form-item prop="qrxpassword" label="确认新密码">
        <el-input
          v-model="form_password.qrxpassword"
          prefix-icon="el-icon-lock"
          show-password
        />
      </el-form-item>
      <el-button type="success" @click="updatePassword">保存</el-button>
    </el-form>
  </div>
</template>
<script>
import request from "@/utils/request";
export default {
  name: "Password",
  data() {
    return {
      labelPosition: "right", // 默认值
      loading: true,
      form_password: {},
      user: {},
      search: "",
    };
  },
  created() {
    var userStr = sessionStorage.getItem("user") || "{}";
    this.user = JSON.parse(userStr);
  },
  methods: {
    updatePassword() {
      this.$refs["form_password"].validate((valiad) => {
        if (valiad) {
          if (
            this.form_password.ypassword == null ||
            this.form_password.ypassword == ""
          ) {
            this.$message.error("请输入原密码");
            return;
          }
          if (
            this.form_password.xpassword == null ||
            this.form_password.xpassword == ""
          ) {
            this.$message.error("请输入新原密码");
            return;
          }
          if (
            this.form_password.qrxpassword == null ||
            this.form_password.qrxpassword == ""
          ) {
            this.$message.error("请输入确认新原密码");
            return;
          }
          if (this.form_password.xpassword != this.form_password.qrxpassword) {
            this.$message.error("两次密码不一致");
            return;
          }
          if (this.form_password.ypassword == this.form_password.xpassword) {
            this.$message.error("新密码与原密码一致");
            return;
          }
          request
            .post(
              "/user/updatePassword?username=" +
                this.user.username +
                "&ypassword=" +
                this.form_password.ypassword +
                "&xpassword=" +
                this.form_password.xpassword
            )
            .then((res) => {
              if (res.code === "0") {
                this.$message({
                  type: "success",
                  message: "修改成功",
                });
                alert("修改成功,请重新登录");
                this.$router.push("/login");
              } else {
                this.$message({
                  type: "error",
                  message: res.msg,
                });
              }
            });
        }
      });
    },
  },
};
</script>
<style scoped></style>
